package org.blog.controller;


import org.blog.entity.Article;
import org.blog.entity.Category;
import org.blog.entity.param.ArticleParam;
import org.blog.entity.param.CategoryParam;
import org.blog.entity.vo.ArticleVO;
import org.blog.service.ArticleService;
import org.blog.service.CategoryService;
import org.blog.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;


    @PostMapping("front/addArticle")
    @ResponseBody
    public ResultResponse addArticle(Article article) {
//        System.out.println(article);
        article.setArticleSendTime(new Date());
        article.setArticleViewNumber(0);
        boolean b = articleService.create(article);
        if (b) {
            return new ResultResponse(true, "保存成功");
        } else {
            return new ResultResponse(false, "保存失败");
        }
    }


    @GetMapping("front/toEditArticle")
    public ModelAndView toEditArticle() {
        ModelAndView modelAndView = new ModelAndView("front/editorArticle");

        List<Category> categorys = categoryService.getCategory(new CategoryParam());
        modelAndView.addObject("categorys", categorys);
        return modelAndView;

    }


    @GetMapping("front/toArticle/{articleId}")
    public ModelAndView toArticle(@PathVariable("articleId") Integer articleId) {
//        System.out.println(articleId);
        ModelAndView modelAndView = new ModelAndView("front/ViewPage");
        articleService.addViewNumber(articleId); // 新增浏览次数
        ArticleVO articleVO = articleService.getArticleFromId(articleId);
        modelAndView.addObject("articleVO", articleVO);
        return modelAndView;
    }


    @GetMapping("admin/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/article");

        // 根据参数获取article
        List<ArticleVO> articles = articleService.getArticleVO(new ArticleParam());

        modelAndView.addObject("list", articles);
        return modelAndView;
    }

    @GetMapping("delete/{articleId}")
    public void delete(@PathVariable("articleId") Integer articleId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean delete = articleService.delete(articleId);
        if (delete) {
            response.sendRedirect("/article/admin/index");
        } else {
            response.getWriter().println("删除失败");
            response.getWriter().close();
        }
    }

    @GetMapping("front/deleteArticle/{articleId}")
    @ResponseBody
    public ResultResponse delete(@PathVariable("articleId") Integer articleId) throws IOException {
        boolean delete = articleService.delete(articleId);
        if (delete) {
            return new ResultResponse(true, "删除成功");
        }else {
            return new ResultResponse(false, "删除失败");
        }
    }


    @PostMapping("search")
    public ModelAndView search( ArticleParam param) {
        ModelAndView modelAndView = new ModelAndView("admin/article");
        List<Article> articles = articleService.getArticles(param);

        modelAndView.addObject("list", articles);
        return modelAndView;
    }


}
